      $ SET SOURCEFORMAT "FREE"
IDENTIFICATION DIVISION.
PROGRAM-ID.  Seq2Index.
AUTHOR.  Raj Akula.
* Creates an indexed file  from a sequential file.

* This is where you declare your enviorment--> didn't really need to declare anything
ENVIRONMENT DIVISION.

*Declare Files here Opening a file that you can read sequentially and one 
* that is indexed, for the indexed defining the key to be CustNo, if there 
* is a dispute with custNo then the customer number is the key, also customer 
* name.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
   SELECT CustomerFile ASSIGN TO "idxcustomers.dat"
          ORGANIZATION IS INDEXED   
          ACCESS MODE IS RANDOM
          RECORD KEY IS CustNo
          ALTERNATE RECORD KEY IS CustomerName
                      WITH DUPLICATES
          FILE STATUS IS CustStatus.

   SELECT SeqCustomerFile ASSIGN TO "customers.dat"
        ORGANIZATION IS LINE SEQUENTIAL.
      
*Declare File Data Records and what is in each file.
DATA DIVISION.
FILE SECTION.
FD CustomerFile.
01 CustomerRecord.
   02 CustNo                  PIC 9(5).  
   02 FILLER                  PIC X(5).  
   02 CustomerName            PIC X(23).
   02 CustAddress.
      03 CustStreet           PIC X(23).
      03 CustCity             PIC X(13).
      03 CustStateCountry     PIC X(12).
   02 OutStandingBal          PIC 99999V99.


FD SeqCustomerFile.
01 SeqCustomerRecord.
   88   EndOfFile VALUE HIGH-VALUES.
   02 SeqCustNo                  PIC 9(5).  
   02 SeqFILLER                  PIC X(5).  
   02 SeqCustomerName            PIC X(23).
   02 SeqCustAddress.
      03 SeqCustStreet           PIC X(23).
      03 SeqCustCity             PIC X(13).
      03 SeqCustStateCountry     PIC X(12).
   02 SeqOutStandingBal          PIC 99999V99.



*Declaring normal variables needed, here the custstatus
* is used to make sure file was made properly using that
* record key
WORKING-STORAGE SECTION.
01   CustStatus              PIC X(2).

*Method to convert file from sequential to indexed
*Basically i am iterating through the file and 
*converting to an indexed file and i am using key
*to be customer number which i decalred in the file 
*open part at the top.
PROCEDURE DIVISION.
Begin.
   OPEN INPUT SeqCustomerFile.
   OPEN OUTPUT CustomerFile.

   READ SeqCustomerFile
      AT END SET EndOfFile TO TRUE
   END-READ.
   PERFORM UNTIL EndOfFile
   MOVE SeqCustomerRecord to CustomerRecord
      WRITE CustomerRecord FROM SeqCustomerRecord
         INVALID KEY DISPLAY "Customer STATUS :- ", CustStatus
      END-WRITE
      READ SeqCustomerFile
         AT END SET EndOfFile TO TRUE
      END-READ
   END-PERFORM.

   CLOSE CustomerFile, SeqCustomerFile.
   STOP RUN.
